package com.ciwong.epaper.modules.me.ui;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.bean.KefuBean;
import com.ciwong.epaper.modules.me.bean.SchoolDetail;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.BaseJumpManager;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * 联系客服
 * Created by Administrator on 2016/9/13.
 */
public class KeFuActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout ll_kf_phone, ll_kf_wechat, ll_kf_wechat_pic;
    private TextView tx_kf_wechat, tx_kf_phone;
    private SimpleDraweeView img_kf_wechat_pic;
    private EApplication application;
    private int mSchoolId;

    @Override
    protected int setView() {
        return R.layout.activity_ke_fu;
    }

    @Override
    protected void findViews() {
        ll_kf_phone = findViewById(R.id.ll_kf_phone);
        ll_kf_wechat = findViewById(R.id.ll_kf_wechat);
        ll_kf_wechat_pic = findViewById(R.id.ll_kf_wechat_pic);
        tx_kf_wechat = findViewById(R.id.tx_kf_wechat);
        tx_kf_phone = findViewById(R.id.tx_kf_phone);
        img_kf_wechat_pic = findViewById(R.id.img_kf_wechat_pic);

    }

    @Override
    protected void initEvent() {

        findViewById(R.id.ll_kf_phone).setOnClickListener(this);

        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                onBackPressed();
            }
        });
    }

    @Override
    protected void init() {
        setTitleText("联系客服");
        application = (EApplication) getApplication();
    }

    @Override
    protected void loadData() {
        getServiceBook();
    }

    private void getServiceBook() {

        SchoolDetail schoolDetail = application.getSchoolDetail();
        final int schoolId = schoolDetail == null ? 0
                : schoolDetail.getSchoolId();
        mSchoolId = schoolId;
        getAdvisor(schoolId);
    }

    /**
     * 获取客服二维码
     *
     * @param schoolId 学校ID
     */
    private void getAdvisor(final int schoolId) {
        EpaperDao.getInstance().getAdvisor(schoolId, EApplication.BRAND_ID, new BaseExtCallBack(KeFuActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                setDefuatWechatImg();
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                setDefuatWechatImg();
            }

            @Override
            public void success(Object data) {
                try {
                    List<KefuBean> kefuBeanList = (List<KefuBean>) data;
                    if (!kefuBeanList.isEmpty()) {
                        KefuBean kefuBean = kefuBeanList.get(0);
                        setDefuatWechatImg(kefuBean);
                    }
                } catch (Exception e) {
                    e.getStackTrace();
                    setDefuatWechatImg();
                }


            }
        });
    }

    /**
     * 设置默认微信图片
     */
    private void setDefuatWechatImg() {
        Uri uri = Uri.parse("https://img-js.imkehou.com/jaadmin/img/xxkefu.jpg");
        img_kf_wechat_pic.setImageURI(uri);
    }

    /**
     * 设置微信图片
     */
    private void setDefuatWechatImg(KefuBean kefuBean) {
        if (kefuBean.getCsQrCode() != null && kefuBean.getCsQrCode() != "") {
            Uri uri = Uri.parse(kefuBean.getCsQrCode());
            img_kf_wechat_pic.setImageURI(uri);
        } else {
            setDefuatWechatImg();
        }
        if (kefuBean.getQq() != null) {
            tx_kf_wechat.setText(kefuBean.getQq());
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ll_kf_phone) {
            //拨打客服电话
            MeJumpManager.jumpToCallPhone(0, KeFuActivity.this, getString(R.string.service_phone));
        }
    }

}
